package com.bojx.cms.web;


import java.util.Date;
import java.util.List;
import com.bojx.cms.render.VelocityLayoutRender;
import com.bojx.cms.service.AuthService;
import com.jfinal.aop.Clear;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;

public class AuthController extends BaseController {
	
	private static AuthService  authService=new AuthService();
	
	public void perm(){
		List<Record> firstPerms=authService.findFirstPerms();
		setAttr("firstPerms", firstPerms);
		render(new VelocityLayoutRender("perm.html"));
	}
	
	public void role(){
		render(new VelocityLayoutRender("role.html"));
	}
	
	public void user(){
		render(new VelocityLayoutRender("user.html"));
	}
	
	@Clear  
	public void permsData(){
		List<Record> perms=authService.findPerms();
		render(new JsonRender(perms).forIE());
	}
	
	public void permUpdate(){
		Record result=new Record();
		try{
			Record permRecord=new Record();
			String id=getPara("id");
			String name=getPara("name");
			String action=getPara("action");
			long  parent_id=getParaToLong("parent_id");
			permRecord.set("name", name);
			permRecord.set("action", action);
			permRecord.set("parent_id",parent_id);
			if(StrKit.isBlank(id)){//新增
				Db.save("cms_perm", permRecord);
			}else{//修改
				permRecord.set("id", id);
				permRecord.set("update_time", new Date());
				Db.update("cms_perm", permRecord);
			}
			result.set("code",0);
			result.set("message", "提交成功");
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result.set("code","100");
			result.set("message", e.getMessage());
		}finally{
			render(new JsonRender(result).forIE());
		}
	}
}
